package com.ligongmusic.util;

import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * 存储相关工具类
 */
public class StorageUtils {
    private Context mContext;
    private static StorageUtils storageUtils;
    private static SharedPreferences sp;

    private StorageUtils(Context mContext) {
        this.mContext = mContext;
        sp = mContext.getSharedPreferences(GlobalParams.sharedPreferencesKey, Activity.MODE_PRIVATE);
    }

    public static StorageUtils getInstance(Context context) {
        if (storageUtils == null)
            storageUtils = new StorageUtils(context);
        return storageUtils;
    }

    public void writeString(String key, String value) {
        Editor edit = sp.edit();
        edit.putString(key, value);
        edit.commit();
    }

    public void writeBoolean(String key, boolean value) {
        Editor edit = sp.edit();
        edit.putBoolean(key, value);
        edit.commit();
    }

    // 通过key获取首选项值
    public <T> T getValue(String key) {
        Map<String, ?> all = sp.getAll();
        return (T) all.get(key);
    }

    public static boolean ExistSDCard() {
        if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
            return true;
        else
            return false;
    }
}
